﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;

namespace ShakePhone
{
    public partial class MainPage : PhoneApplicationPage
    {
        
        Accelerometer accelerometer;
        int delta;
        double itime = 0;
        double dy = 100;
        double dv = 0;
        double dAcceleX = 0;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            accelerometer = new Accelerometer();
            delta = 0;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(accelerometer_ReadingChanged);
            accelerometer.Start();
            //-----------------nguyen huu toangdsaewgewagfewgfvaewf
        }

        void accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(()=>chagne(e));
        }
        void chagne(AccelerometerReadingEventArgs e)
        {
            textBox1.Text = "x = " + e.X;
            textBox2.Text = "y = " + e.Y;
            textBox3.Text = "z = " + e.Z;

            if (Math.Abs(dAcceleX - e.X) < 0.0001)
            {
                
                if (e.Y > 0)
                {
                    dy += dv * itime + 1 / 2 * 9.98 * e.X * itime * itime;
                    dv += 9.98 * e.X * itime;
                }
                else
                {
                    dy += dv * itime - 1 / 2 * 9.98 * e.X * itime * itime;
                    dv += -9.98 * e.X * itime;
                }

                itime += 0.000033;
                
            }
            else
            {
                itime = 0;
                dAcceleX = e.X;

                if (e.Y > 0)
                {
                    dv += 9.98 * e.X * itime;
                }
                else
                {
                    dv += -9.98 * e.X * itime;
                }
            }

            ellipse1.Margin = new Thickness(0, dy, 0, 601 - 50 - dy);
            
        }
    }
}